package com.chazz.dao.impl;

import com.chazz.dao.CartDao;
import com.chazz.dao.GoodsDao;
import com.chazz.dao.ShoppingcarDao;
import com.chazz.entity.Cart;
import com.chazz.entity.Goods;
import com.chazz.entity.Shoppingcar;
import com.chazz.utils.CountCartPrice;
import com.chazz.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl extends GenericDaoImpl<Cart,Integer> implements CartDao {
    public static List<Cart> carts = new ArrayList();
//    static {
//        Cart cart1 = new Cart(0,0,1,1,null);
//        Cart cart2 = new Cart(0,0,2,1,null);
//        Cart cart3 = new Cart(0,0,3,1,null);
//        carts.add(cart1);
//        carts.add(cart2);
//        carts.add(cart3);
//    }
    ShoppingcarDao shoppingcarDao = new ShoppingcarDaoImpl();
    @Override
    public void cartGoodAdd(Integer goodId,Integer userId) {
        System.out.println("添加被调用");
        String tatolPrice = String.valueOf(CountCartPrice.onlyPrice(goodId));
        Shoppingcar shoppingcar = shoppingcarDao.findByUid(userId);
        Integer sid = shoppingcar.getId();
        List<Cart> carts = findAll();
        if (carts != null) {
            for (Cart c : carts) {
                if (c.getGid() == goodId) {
                    //修改商品数量
                    Integer count = c.getCount();
                    System.out.println(count);
                    count += 1;
                    String sql = "UPDATE Cart SET count = ? WHERE gid = ?";
                    Object[] param = new Object[]{
                            count,
                            goodId
                    };
                    System.out.println("添加后" + count);
                    DBUtils.executeUpdate(sql, param);
                    break;
                } else if (c == carts.get(carts.size() - 1) && c.getGid() != goodId) {
                    String sql = "INSERT INTO cart(sid,gid,count,tatolPrice)VALUES(?,?,?,?)";
                    Object[] param = new Object[]{
                            sid,
                            goodId,
                            1,
                            tatolPrice
                    };
                    DBUtils.executeUpdate(sql, param);
                } else {
                    continue;
                }
            }
        }else{
            String sql = "INSERT INTO cart(sid,gid,count,tatolPrice)VALUES(?,?,?,?)";
            Object[] param = new Object[]{
                    sid,
                    goodId,
                    1,
                    tatolPrice
            };
            DBUtils.executeUpdate(sql, param);
        }
    }


//    @Override
//    public Cart fingCartBySid(Integer sid) {
//        Cart cart = null;
//        String sql = "SELECT * FROM Cart WHERE SID=?";
//        ResultSet rs = DBUtils.executeQuery(sql,sid);
//        try{
//            while(rs.next()){
//                cart = reflectUtils.newInstance(Cart.class,rs);
//            }
//        }catch (SQLException ex){
//            ex.printStackTrace();
//        }
//        return cart;
//    }

    @Override
    public List<Cart> findCartBySid(Integer sid) {
        List<Cart> carts = new ArrayList<Cart>();
        String sql = "SELECT * FROM Cart WHERE sid=?";
        ResultSet rs = DBUtils.executeQuery(sql,sid);
        try{
            while(rs.next()){
                Cart cart = reflectUtils.newInstance(Cart.class,rs);
                carts.add(cart);
            }
        }catch(
                SQLException ex){
            ex.printStackTrace();
        }
        return carts;
    }

    @Override
    public void deleteByGid(Integer gid) {
        String sql = "DELETE FROM Cart WHERE gid=?";
        Integer row = DBUtils.executeUpdate(sql,gid);
    }

    @Override
    public void updateCartCount(Integer count , Integer cartid) {
        String sql = "UPDATE Cart SET Count=? WHERE id=?";
        Object[] param = new Object[]{
            count,
            cartid
        };
        DBUtils.executeUpdate(sql,param);
    }
    //未登录时 购物车的操作
    @Override
    public void cartGoodAdd(Integer goodId) {
        System.out.println("添加被调用");
        String tatolPrice = String.valueOf(CountCartPrice.onlyPrice(goodId));
        //新建一个userid为0 的shoppingcar
        Shoppingcar shoppingcar = new Shoppingcar(0, 0, 0, null);
        Integer sid = shoppingcar.getId();
        if(carts!=null){
            for(Cart c : carts){
                if(goodId==c.getGid()){
                    c.setCount(c.getCount()+1);
                }
            }
        }else{
            Cart cart = new Cart(0,0,goodId,1,null);
            carts.add(cart);
        }
    }

    public List<Cart> getCarts(){
        return carts;
    }

    public void clear(){
        carts.clear();
    }



    @Override
    public void cartGoodAdd(Integer goodid, Integer count, Integer userid) {
        Shoppingcar shoppingcar = shoppingcarDao.findByUid(userid);
        Cart cart = new Cart(userid,shoppingcar.getId(),goodid,count,"0");
        List<Cart> carts = findAll();
        if(carts!=null){
            for(Cart c : carts) {
                if (c.getGid() == goodid) {
                    //修改商品数量
                    Integer cou = c.getCount();
                    System.out.println(count);
                    String sql = "UPDATE Cart SET count = ? WHERE gid = ?";
                    Object[] param = new Object[]{
                            count+cou,
                            goodid
                    };
                    System.out.println("添加后" + count);
                    DBUtils.executeUpdate(sql, param);
                    break;
                } else if (c == carts.get(carts.size() - 1) && c.getGid() != goodid) {
                    String sql = "INSERT INTO cart(sid,gid,count,tatolPrice)VALUES(?,?,?,?)";
                    Object[] param = new Object[]{
                            cart    .getSid(),
                            goodid,
                            count,
                            cart.getTatolprice()
                    };
                    DBUtils.executeUpdate(sql, param);
                } else {
                    continue;
                }
            }
        }else{
            String sql = "INSERT INTO cart(sid,gid,count,tatolPrice)VALUES(?,?,?,?)";
            Object[] param = new Object[]{
                    cart.getSid(),
                    goodid,
                    count,
                    cart.getTatolprice()
            };
            DBUtils.executeUpdate(sql, param);
        }
    }
}
